package com.android.djlmapp.activity.kaika;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.activity.kaika.web.HeYueJiYeWuActivity;
import com.android.djlmapp.adapter.kaika.KaiKaAdapter;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.http.GlobalUrls;
import com.android.djlmapp.model.kaika.KaiKaHomeInfo;
import com.android.djlmapp.tools.global.ConstantsValue;
import com.android.djlmapp.tools.global.PrefUtils;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.readcard.eventbus.Event;
import com.android.readcard.eventbus.EventCodes;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class KaiKaActivity extends BaseActivity {


    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    @BindView(R.id.page_title_layout)
    ConstraintLayout pageTitleLayout;
    @BindView(R.id.lv_kaika)
    ListView lvKaika;
    private KaiKaAdapter kaiKaAdapter;
    private int isJITuan = 0;
    private static final int REQUEST_CODE = 222;
    private ArrayList<KaiKaHomeInfo.ActionBean> list = new ArrayList();
    private String yewuType;
    private String kaikadata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kai_ka);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        llTopBack.setVisibility(View.VISIBLE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.VISIBLE);
        tvName.setText("手机开卡");
        kaikadata = PrefUtils.getString("KAIKADATA", "");
    }

    @Override
    protected void initData() {
        PrefUtils.putString("BluetoothName", ""); // 充值蓝牙名称信息
        kaiKaAdapter = new KaiKaAdapter(KaiKaActivity.this);
        lvKaika.setAdapter(kaiKaAdapter);


        lvKaika.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (list.size() > 0) {
                    String jumpFlag = list.get(position).getAction(); // 获取数据返回的跳转字段
                    yewuType = list.get(position).getYewu_type();
                    jumpActvity(jumpFlag);
                } else {
                    ToastUtils.showShortToast(KaiKaActivity.this, "数据请求异常");
                }
            }
        });
        if (!TextUtils.isEmpty(kaikadata)) {
            Log.e("项目异常", "------缓存数据------");
            parseJson(kaikadata);
        } else {
            Log.e("项目异常", "------缓存数据为空111------");
            kaiKaAdapter.setData(1, list);
        }
        MyApplication.getInstance().getHttpTools().getKaiKaHomeData(GlobalUrls.GET_KAIKA_HOME_DATA,
                new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            String body = response.body();
                            parseJson(body);
                        } catch (Exception e) {
                            ToastUtils.showShortToast(KaiKaActivity.this, "数据解析异常");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                        String body = response.body();
                        Log.e("首页数据", "-----onError------" + body);
                        ToastUtils.showShortToast(KaiKaActivity.this, "网络或服务器错误");
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });

    }

    private void parseJson(String json) {
        Gson gson = new Gson();
        KaiKaHomeInfo kaiKaHomeInfo = gson.fromJson(json, KaiKaHomeInfo.class);
        if (kaiKaHomeInfo != null) {
            int code = kaiKaHomeInfo.getCode();
            if (code < 1000) {
                String is_input_idcard = kaiKaHomeInfo.getIs_input_idcard();

                is_input_idcard = kaiKaHomeInfo.getIs_input_idcard();

                if ("2".equals(kaiKaHomeInfo.getIdcard_type())) {
                    PrefUtils.putString(ConstantsValue.ISINPUTHUZHAO, kaiKaHomeInfo.getIdcard_type()); // 保存是否有护照
                } else {
                    PrefUtils.putString(ConstantsValue.ISINPUTHUZHAO, ""); // 保存是否有护照
                }

                if (is_input_idcard != null && !TextUtils.isEmpty(is_input_idcard)) {
                    PrefUtils.putString(ConstantsValue.ISINPUTIDCARD, is_input_idcard); // 保存是否可以手动拍照
                } else {
                    PrefUtils.putString(ConstantsValue.ISINPUTIDCARD, ""); // 保存是否可以手动拍照
                }
                List<KaiKaHomeInfo.ActionBean> action = kaiKaHomeInfo.getAction();
                if (action != null && action.size() > 0) {
                    list = new ArrayList();
                    PrefUtils.putString("KAIKADATA", json);
                    list.addAll(action);
                    kaiKaAdapter.setData(1, list);
                } else {
                    ToastUtils.showShortToast(KaiKaActivity.this, "未获取到数据!");
                }
            } else {
                ToastUtils.showShortToast(KaiKaActivity.this, kaiKaHomeInfo.getMsg());
            }
        } else {
            ToastUtils.showShortToast(KaiKaActivity.this, "未获取到数据");
        }

    }

    public void jumpActvity(String jumpFlag) {

        switch (jumpFlag) {
            case ConstantsValue.DKYW: // 单卡业务
                startActivity(new Intent(KaiKaActivity.this, SelectNumAttestationActivity.class).putExtra("YEWUTYPE", yewuType));
                break;
            case ConstantsValue.JTYW: // 集团业务
                isJITuan = 1;
                Intent intent = new Intent(KaiKaActivity.this, KaiKaPopupWindowActivity.class);
                intent.putExtra("isJiTuan", isJITuan);
                startActivityForResult(intent, REQUEST_CODE);
                overridePendingTransition(R.anim.push_xiao_da_in, R.anim.push_xiao_da_out);
                break;
            case ConstantsValue.HYJYW: // 合约机业务
                startActivity(new Intent(KaiKaActivity.this, HeYueJiYeWuActivity.class).putExtra("YEWUTYPE", yewuType));
                break;
            case ConstantsValue.CKJH: //成卡激活
                startActivity(new Intent(KaiKaActivity.this, ChengKaJiHuoActivity.class).putExtra("YEWUTYPE", yewuType));
                break;
            case ConstantsValue.WXGH: // 无线固话
                isJITuan = 2;
                Intent intent2 = new Intent(KaiKaActivity.this, KaiKaPopupWindowActivity.class);
                intent2.putExtra("isJiTuan", isJITuan);
                startActivityForResult(intent2, REQUEST_CODE);
                overridePendingTransition(R.anim.push_xiao_da_in, R.anim.push_xiao_da_out);
                break;
            case ConstantsValue.FKBL: // 副卡办理
                isJITuan = 3;
                Intent intent3 = new Intent(KaiKaActivity.this, FuKaBanLiActivity.class);
                startActivityForResult(intent3, REQUEST_CODE);
                overridePendingTransition(R.anim.push_xiao_da_in, R.anim.push_xiao_da_out);
                break;
        }
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(Event event) {
        switch (event.getCode()) {
            case EventCodes.EventKaiKaCode.D: // 提交订单之后关闭开卡界面
                finish();
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            if (data != null) {
                String jtbm_result = data.getStringExtra("JTBM_RESULT"); // 获取到集团编码,固话号码
                String addpara = data.getStringExtra("ADDPARA_RESULT"); // 获取addpara

                Log.e("项目异常", jtbm_result + "------onActivityResult------" + addpara + "------" + isJITuan);
                if (jtbm_result != null && !TextUtils.isEmpty(jtbm_result)) {

                    if (isJITuan == 1) {
                        Intent intent = new Intent(KaiKaActivity.this, SelectNumAttestationActivity.class);
                        intent.putExtra("YEWUTYPE", yewuType);
                        intent.putExtra("JTBM", jtbm_result); // 是集团编码就只传集团编码
                        startActivity(intent);
                    } else if (isJITuan == 2) {
                        Intent intent = new Intent(KaiKaActivity.this, SelectNumAttestationActivity.class);
                        intent.putExtra("GHHM", jtbm_result); // 是固话就传固话号码
                        intent.putExtra("YEWUTYPE", yewuType);
                        if (jtbm_result != null && !TextUtils.isEmpty(jtbm_result)) {
                            intent.putExtra("ADDPARA", addpara); // 是固话就传addpara
                        } else {
                            ToastUtils.showShortToast(KaiKaActivity.this, "未获取到相关数据.");
                        }
                        startActivity(intent);
                    } else if (isJITuan == 3) {
                        Intent intent = new Intent(KaiKaActivity.this, SelectNumAttestationActivity.class);
                        intent.putExtra("YEWUTYPE", "1");
                        intent.putExtra("ADDPARA", "{\"main_phone\":\"" + jtbm_result + "\"}"); // 是固话就传addpara
                        intent.putExtra("FKBL_NUM", jtbm_result); // 是集团编码就只传集团编码
                        startActivity(intent);
                    }

                } else {
                    ToastUtils.showShortToast(KaiKaActivity.this, "未获取到相关数据");
                }
            }
        }
    }
}
